﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Text.RegularExpressions;
using System.IO;
using System.Collections;
using System.Globalization;
using System.Threading;

namespace webcontent
{
    public partial class Form1 : Form
    {
        static private Regex regex;
        static int count;
        private ArrayList https;
        static ArrayList results;
  
        public Form1()
        {
           
            count = 0;
            https = new ArrayList();
            results = new ArrayList();

            InitializeComponent();
            //button2_Click(null, null);
        }

      


        //由"../关键字.txt"获取正则表达式
        private void readRegex()
        {
            string[] lines = File.ReadAllLines("../关键字.txt", System.Text.Encoding.Default);
            string keys = lines[0];
            regex = new Regex("^.*" + keys + ".*$");
        }

        private void button2_Click(object sender, EventArgs e)
        {

            //读取正则表达式
            readRegex();
            //读取地址
            readWebAddress();


            int i = 0;
            foreach (string[] http in https)
            {
                i++;
                this.startDownload(http);
                
                //string str = getContent(http);

            }
            Thread.Sleep(1000);
            Application.Exit();

        }

        //根据 “网址.txt”得到http地址
        private void readWebAddress()
        {
            string[] lines = File.ReadAllLines("../网址.txt", System.Text.Encoding.Default);
            foreach (string line in lines)
            {
                string[] splitLine = new string[3];
                splitLine = line.Split('|');
                this.https.Add(splitLine);
            }
        }

        //根据地址得到网页内容
        private void startDownload(string[] httpLine)
        {

            String url = httpLine[2];

            //WebClient myWebClient = new WebClient();
            try
            {
                DownloadAsync download = new DownloadAsync();
                download.DownloadCompleted += new DownloadDelegate(download_DownloadCompleted);
                download.Downloadasync(httpLine[2], httpLine);
                //byte[] myDataBuffer = myWebClient.DownloadDataAsync(url);                
            }
            catch (Exception e)
            {
                Console.WriteLine("---web地址有误---" + httpLine[2]);
                Log.log("---web地址有误---" + httpLine[2]);
                Console.WriteLine(e.ToString());
            }


        }

        //比较网页内容
        static private void compare(string webcontent, string[] httpLine)
        {
            
            string[] datas = webcontent.Split(new string[] { "\n" }, StringSplitOptions.RemoveEmptyEntries);
            Console.WriteLine("------网页源码行数=" + datas.Length);
            Log.log("------网页源码行数=" + datas.Length);
            foreach (string data in datas)
            {
                MatchCollection mc = regex.Matches(data);
                foreach (Match m in mc)
                {
                    Console.WriteLine("名称:" + httpLine[0] + "地址：" + httpLine[2]);
                    Console.Write(m.Value.ToString());
                    Console.Write("  in :");
                    Console.WriteLine(data);

                    Log.log("名称:" + httpLine[0] + "地址：" + httpLine[2]);
                    Log.log(m.Value.ToString());
                    Log.log("  in :");
                    Log.log(data);


                    string temp = "名称:" + httpLine[0] + "地址：" + httpLine[2] + "\n\r" + m.Value.ToString() + " in " + data;
                    results.Add(temp);
                    //return;
                }

            }
        }

        static void download_DownloadCompleted(byte[] myDataBuffer, string[] httpLine)
        {
            try
            {
                count++;
                Thread.Sleep(2 * 1000);
                Console.WriteLine("下载完毕----" + count + "---" + httpLine[0] + "   " + httpLine[2]);
                Log.log("下载完毕----" + count + "---" + httpLine[0] + "   " + httpLine[2]);
                string charset = httpLine[1];
                string strWebData = "";
                if (charset != null && !charset.Equals(""))
                    strWebData = Encoding.GetEncoding(charset).GetString(myDataBuffer);
                else
                {
                    Console.WriteLine("charset缺失");
                    Log.log("charset缺失");
                }

                if (strWebData.Length > 0)
                {
                    compare(strWebData, httpLine);

                }
            }
            catch (Exception e)
            {
                Console.WriteLine("下载响应异常："+e.ToString());
                Log.log("下载响应异常：" + e.ToString());
            }
        }

        public delegate void DownloadDelegate(byte[] b, string[] httpLine);

        public class DownloadAsync
        {
            public event DownloadDelegate DownloadCompleted;
            byte[] buf;
            string[] httpLine;

            public void Downloadasync(string url, string[] httpLine)
            {
                this.httpLine = httpLine;
                WebClient webClient = new WebClient();
                webClient.DownloadDataCompleted += new DownloadDataCompletedEventHandler(webClient_DownloadDataCompleted);
                webClient.DownloadDataAsync(new Uri(url));
            }

            void webClient_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
            {
                buf = e.Result;
                if (DownloadCompleted != null)
                    DownloadCompleted(buf, httpLine);
            }
        }
    }
}
